from time import sleep

from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

class BaseFunction:
    def find(self, by, locator=''):
        # 判断传入的类型是否为元组类型
        if isinstance(by, tuple):
            return self._driver.find_element(*by)
        else:
            return self._driver.find_element(by, locator)

    def wait(self, by, locator=''):
        if isinstance(by, tuple):
            return WebDriverWait(self._driver, 10).until(
                expected_conditions.element_to_be_clickable(by)
            )
        else:
            return WebDriverWait(self._driver, 10).until(
                expected_conditions.element_to_be_clickable((by, locator))
            )

    def wait_hide(self, by, locator=''):
        if isinstance(by, tuple):
            return WebDriverWait(self._driver, 10).until_not(
                expected_conditions.element_to_be_clickable(by)
        )
        else:
            return WebDriverWait(self._driver, 10).until_not(
                expected_conditions.element_to_be_clickable((by, locator))
            )

    def close(self):
        sleep(20)
        self._driver.quit()
